package com.jcxt.sys.common.code_generate;

import static java.lang.Character.*;

/**
 * @author 邱理
 * @description
 * @date 创建于 2019/4/20
 */
public interface PropertyNameMapper {

    String keyNameToClassPropertyName(String keyName);

    PropertyNameMapper NoOp = new PropertyNameMapper() {
        @Override
        public String keyNameToClassPropertyName(String keyName) {
            return keyName;
        }
    };

    PropertyNameMapper UnderLineToCamel = new PropertyNameMapper() {
        @Override
        public String keyNameToClassPropertyName(String keyName) {
            StringBuilder sb = new StringBuilder();
            char[] arr = keyName.toCharArray();
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] == '_') {
                    sb.append(toUpperCase(arr[++i]));
                } else {
                    sb.append(arr[i]);
                }
            }
            return sb.toString();
        }
    };

    PropertyNameMapper CamelToUnderLine = new PropertyNameMapper() {

        @Override
        public String keyNameToClassPropertyName(String keyName) {
            StringBuilder sb = new StringBuilder();
            char[] arr = keyName.toCharArray();
            for (int i = 0; i < arr.length; i++) {
                if (isUpperCase(arr[i])) {
                    sb.append("_").append(toLowerCase(arr[i]));
                } else {
                    sb.append(arr[i]);
                }
            }
            return sb.toString();
        }
    };
}
